home *** CD-ROM | disk | FTP | other *** search
- Path: engnews2.Eng.Sun.COM!usenet
- From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
- Newsgroups: comp.lang.c++
- Subject: Re: Multiple Abstract Classes Question
- Date: 18 Jan 1996 22:18:13 GMT
- Organization: SunSoft
- Message-ID: <NITIN.96Jan18141813@more.eng.sun.com>
- References: <1996Jan17.160425.6063@ned.cray.com>
- NNTP-Posting-Host: more.eng.sun.com
- In-reply-to: aztec@cray.com's message of 17 Jan 96 16:04:25 CST
-
-
- In article <1996Jan17.160425.6063@ned.cray.com> aztec@cray.com (Joel Garcia-Trevino {x66457 CF/DEV}) writes:
-
- > Newsgroups: comp.lang.c++
- > From: aztec@cray.com (Joel Garcia-Trevino {x66457 CF/DEV})
- > Reply-To: aztec@cray.com
- > Organization: Cray Research, Inc.
- > Date: 17 Jan 96 16:04:25 CST
- >
- >
- > Can I or is it a good idea to have multiple abstract classes to start building
- > a class hierarchy?
- >
-
- Yes, if you want to keep it flexible. But if you design your
- classes properly, you can still keep them flexible without
- having the abstract class defined for your "type" class. I
- don't even see a need for making your "base_net" class
- abstract! I feel the methods get_name() & get_type(), don't
- need to be pure virtual at all. These are accessor methods
- and if you keep the attributes they are accessing in the same
- class where these methods are defined, they don't need to be
- pure virtual methods. This is how you can change your
- classes:
-
- 1. Move attributes net::name and net::type_pntr to the class
- base_net where the accessors are defined.
-
- 2. Change the accessor methods to return the corresponding
- attribute. You may not even have to make these methods
- virtual but if you want the derived classes to redefine
- these, you can keep them virtual.
-
- 3. Provide a constructor in the base_net class which takes
- name and type as arguments. Call this constructor from
- the constructors of the derived class. Provide default
- values if you want.
-
- 4. Optionally, provide mutator methods to change the
- attributes. You could even combine the accessors and
- mutators into one by returing a reference to the
- attribute.
-
- 5. If you still want the base_net class to be abstract (so
- that nobody can instantiate it directly) and if you don't
- have any other pure virtual methods, you can always put
- the constructors for the base_net class in the protected
- section so that only the derived classes can access them.
-
- 6. You may differ until later whether you really need
- abstract class for type. There won't be lot of changes
- to do that. So you could start with non-abstract version
- first.
-
-
- By defining the return type of the methods, you have already
- decided on the types of the attributes which your derived
- classes will have. Why not then store them in the base class
- itself?
-
- Hope this helps.
-
- -Nitin
-
-
-
-
- > Example of the base (abstract) classes:
- >
- > class base_type;
- >
- > class base_net {
- > public:
- > virtual char *get_name() = 0; // Gets the name
- > virtual base_type *get_type() = 0; // Gets pointer to type
- > };
- >
- > class base_type {
- > public:
- > virtual char *get_type() = 0; // Gets type name
- > };
- >
- >
- > The final classes would look something like:
- >
- > class type;
- >
- > class net: public base_net {
- > private:
- > char *name;
- > type *type_pntr;
- >
- > public:
- > char *get_name() { ..... };
- > type *get_type() { ..... };
- >
- > };
- >
- > class type: public base_type {
- > private:
- > char *type_name;
- >
- > public:
- > char *get_type() { ..... };
- >
- > };
- >
- >
- > Does it make sense to do it this way?
- >
- > The reason why I want to do this is because there will be different types
- > of nets that I will need to work with. I don't know if there will be
- > different types of "type" but I thought why not make them all abstract. ???
- >
- >
- > Thanks in advance for your help,
- >
- >
- > Joel Garcia
- >
- >
- >
- --
- ----------------------------------------------------------------------
- Nitin More
- SunSoft, Bldg 16 Off: (415) 786 7109
- Menlo Park, CA Fax: (415) 786 7957 e-mail: nitin@more.eng.sun.com
- ----------------------------------------------------------------------
-